package com.ruoyi.common.core.utils;

import cn.hutool.Hutool;
import cn.hutool.crypto.SecureUtil;
import cn.hutool.http.HttpRequest;
import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.ruoyi.common.core.domain.AliasListVO;
import com.ruoyi.common.core.domain.BatchMessageVO;
import com.ruoyi.common.core.domain.GeTuiResult;
import com.ruoyi.common.core.domain.SingleMessageVO;
import org.apache.poi.ss.formula.functions.T;
import sun.security.provider.SHA;

import java.util.HashMap;
import java.util.Map;
import java.util.logging.Logger;

public class GeTuiUtils {

    /**
     * 获取个推的token
     * @param appkey
     * @param masterSecret
     * @param url
     * @return
     */
    public static GeTuiResult getToken(String appkey, String masterSecret, String baseUrl){
        String url = baseUrl + "/auth";
        String timestamp = getTimestamp();
        String sign = SecureUtil.sha256(appkey + timestamp + masterSecret);
        Map<String, String> mp = new HashMap<>();
        mp.put("sign", sign);
        mp.put("timestamp", timestamp);
        mp.put("appkey", appkey);
        String request = JSONUtil.toJsonStr(mp);
       String resultStr = HttpRequest.post(url).header("content-type","application/json;charset=utf-8").body(request).execute().body();
       GeTuiResult result = JSON.parseObject(resultStr, GeTuiResult.class);
       return result;
    }


    /**
     * 单个推送消息
     *
     * @param url
     * @param token
     * @param singleMessageVO
     * @return
     */
    public static GeTuiResult singlePush(String baseUrl, String token, SingleMessageVO singleMessageVO, int type){
        String url = "";
        if (type == 0){
            url = baseUrl + "/push/single/cid";
        }else {
            url = baseUrl + "/push/single/alias";
        }
        String request = JSONUtil.toJsonStr(singleMessageVO);
        String resultStr = HttpRequest.post(url).header("token", token)
                .header("content-type","application/json;charset=utf-8")
                .body(request).execute().body();
        return JSON.parseObject(resultStr, GeTuiResult.class);
    }

    /**
     * 批量推送
     * @param url
     * @param token
     * @param batchMessageVO
     * @return
     */
    public static GeTuiResult batchPush(String baseUrl, String token, BatchMessageVO batchMessageVO, int type){
        String url = "";
        if (type == 0){
            url = baseUrl + "/push/single/batch/cid";
        }else {
            url = baseUrl + "/push/single/batch/alias";
        }
        String request = JSONUtil.toJsonStr(batchMessageVO);
        String resultStr = HttpRequest.post(url).header("token", token)
                .header("content-type","application/json;charset=utf-8")
                .body(request).execute().body();
        return JSON.parseObject(resultStr, GeTuiResult.class);
    }


    /**
     * 绑定别名
     * @param baseUrl
     * @param token
     * @param listVO
     * @return
     */
    public static GeTuiResult setAlias(String baseUrl, String token, AliasListVO listVO){
        String url = baseUrl + "/user/alias" ;
        String request = JSONUtil.toJsonStr(listVO);
        String resultStr = HttpRequest.post(url).header("token", token)
                .header("content-type","application/json;charset=utf-8")
                .body(request).execute().body();
        return JSON.parseObject(resultStr, GeTuiResult.class);
    }



    public static String getTimestamp() {
        return System.currentTimeMillis() + "";
    }

//    public static void main(String[] args) {
//        System.out.println(getTimestamp());
//    }
}
